package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> setmealPage = (Page<SetmealVO>) setmealMapper.pageList(setmealPageQueryDTO);

        List<SetmealVO> result = setmealPage.getResult();
        for (SetmealVO setmealVO : result) {
            CategoryDTO dto = new CategoryDTO();
            dto.setId(setmealVO.getCategoryId());
            Category category = categoryMapper.findByCondition(dto);

            setmealVO.setCategoryName(category.getName());
        }
        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);

        CategoryDTO dto = new CategoryDTO();
        dto.setId(setmeal.getCategoryId());
        Category category = categoryMapper.findByCondition(dto);

        List<SetmealDish> setmealDishList = setmealDishMapper.findBySetmealId(id);

        return SetmealVO.builder()
                .categoryId(setmeal.getCategoryId())
                .categoryName(category.getName())
                .description(setmeal.getDescription())
                .id(setmeal.getId())
                .image(setmeal.getImage())
                .name(setmeal.getName())
                .price(setmeal.getPrice())
                .status(setmeal.getStatus())
                .updateTime(setmeal.getUpdateTime())
                .setmealDishes(setmealDishList)
                .build();
    }

    /**
     * 套餐起售、停售
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);
        for (SetmealDish setmealDish : setmealDishes) {
            Dish dish = dishMapper.findById(setmealDish.getDishId());
            if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.updateStatus(setmeal);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        SetmealPageQueryDTO dto = new SetmealPageQueryDTO();
        dto.setName(setmealDTO.getName());
        List<SetmealVO> setmealVOS = setmealMapper.pageList(dto);
        if (setmealVOS != null && !setmealVOS.get(0).getId().equals(setmealDTO.getId())) {
            throw new BaseException("套餐已存在，请更换套餐名称");
        }

        Setmeal setmeal = Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .status(StatusConstant.ENABLE)
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .build();

        setmealMapper.add(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() != 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.add(setmealDish);
            }
        }
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateInfo(SetmealDTO setmealDTO) {
        setmealDishMapper.delete(setmealDTO.getId());

        Setmeal setmeal = Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .status(setmealDTO.getStatus())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .id(setmealDTO.getId())
                .build();

        SetmealPageQueryDTO dto = new SetmealPageQueryDTO();
        dto.setName(setmealDTO.getName());
        List<SetmealVO> setmeals = setmealMapper.pageList(dto);
        if (setmeals.size() != 0 && !setmeals.get(0).getId().equals(setmealDTO.getId())) {
            throw new BaseException("套餐名不能重复");
        }

        setmealMapper.updateInfo(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.add(setmealDish);
        }
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            setmealDishMapper.delete(id);
        }

        setmealMapper.delete(ids);
    }
}
